package com.unicorn.util.interceptors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;

import java.util.Iterator;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/28.
 */

public class UnicornLogInterceptor implements WebRequestInterceptor {

    private static Logger logger = LoggerFactory.getLogger(UnicornLogInterceptor.class);


    @Override
    public void preHandle(WebRequest webRequest) throws Exception {
        logger.info("request headInfo stack:------------->");
        for(Iterator<String> itr=webRequest.getHeaderNames();itr.hasNext();){
            String name=itr.next();

            logger.info("name="+name+" value is "+webRequest.getHeader(name));
        }
    }

    @Override
    public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {
        logger.info("postHandle.............");
        if(modelMap!=null) {
            for (Iterator<Map.Entry<String, Object>> itr = modelMap.entrySet().iterator(); itr.hasNext(); ) {
                Map.Entry entry = itr.next();
                logger.info("key" + entry.getKey().toString() + "=" + entry.getValue().toString());
            }
        }
    }

    @Override
    public void afterCompletion(WebRequest webRequest, Exception e) throws Exception {
        logger.info("afterCompletion.............");
    }
}
